package com.geovis.emergency.spd.controller.pc;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.geovis.common.core.api.Result;
import com.geovis.common.core.controller.BaseController;
import com.geovis.common.mybatis.page.PageParam;
import com.geovis.common.mybatis.page.PageResult;
import com.geovis.emergency.spd.biz.command.service.ITEpDisasterService;
import com.geovis.emergency.spd.biz.pc.service.IPcDrillDisasterEventService;
import com.geovis.emergency.spd.entity.command.pojo.dto.TepDisasterReqDTO;
import com.geovis.emergency.spd.entity.command.pojo.vo.TepDisasterEventVO;
import com.geovis.emergency.spd.entity.pc.entity.PcDrillDisasterEvent;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.License;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/pc/tep/disaster")
@RequiredArgsConstructor
@Api(value = "应急预案指挥模块-指挥大屏事件", tags = "应急预案指挥模块-指挥大屏事件")
@Slf4j
@Validated
public class PcTepDisasterController extends BaseController<ITEpDisasterService> {

    private final IPcDrillDisasterEventService pcDrillDisasterEventService;

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "事件列表", notes = "演练报告数据导出接口")
    @PostMapping("eventList")
    public Result<List<TepDisasterEventVO>> eventList(){
        return Result.ok(baseService.selectEventList(Wrappers.lambdaQuery(TepDisasterEventVO.class)));
    }
    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "事件列表", notes = "演练报告数据导出接口")
    @PostMapping("listPage")
    public Result<PageResult<TepDisasterEventVO>> listPage(@RequestBody @Validated PageParam<TepDisasterReqDTO> pageParam){
        TepDisasterReqDTO reqDTO=pageParam.getQuery();
        List<PcDrillDisasterEvent> events=pcDrillDisasterEventService.list(Wrappers.lambdaQuery(PcDrillDisasterEvent.class).eq(PcDrillDisasterEvent::getPcDrillId, reqDTO.getPcDrillId()).isNotNull(PcDrillDisasterEvent::getSchedulingEventId));
        List<String> schedulingEventIds=null;
        if(!CollectionUtils.isEmpty(events))
        {
            schedulingEventIds=events.stream().map(PcDrillDisasterEvent::getSchedulingEventId).collect(Collectors.toList());
        }
        PageResult<TepDisasterEventVO> tepDisasterEventVOPageResult = baseService.selectEventPage(pageParam);
        for (TepDisasterEventVO record : tepDisasterEventVOPageResult.getRecords()) {
            //设置状态
            if(null!=schedulingEventIds)
            {
                if(schedulingEventIds.contains(record.getId()))
                {
                    record.setStatus("2");
                }
                else
                {
                    record.setStatus("1");
                }
            }
            else
            {
                record.setStatus("1");
            }
        }

        return Result.ok(tepDisasterEventVOPageResult);
    }
}
